/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.util;

import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.component.gateway.core.util.HexUtil;
import info.peigen.hotpot.component.gateway.dianlan.annotations.DianlanProtocolParse;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanFrameSign;
import info.peigen.hotpot.component.gateway.dianlan.protocol.Dianlan;
import lombok.extern.slf4j.Slf4j;
import org.smartboot.socket.transport.AioSession;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * <b>(DianlanUtil)</b>
 *
 * @author czj
 * @version 1.0.0
 * @since 2022/11/30
 */
@Slf4j
public class DianlanUtil {
    public static void write(AioSession session, Dianlan dianlan) {
        log.info(JSON.toJSONString(dianlan));
        dianlan.setLength(dianlan.dataLength());
        dianlan.setData(HexUtil.hexToBinary(dianlan.getData()));
        dianlan.setFrameCheck(dianlan.check());
        dianlan.setData(HexUtil.binaryToHex(dianlan.getData()));
        byte[] response = DianlanProtocolParse.serialization(dianlan);
        String result   = HexUtil.binaryToHex(new String(response));
        byte[] b        = HexUtil.hexStringToByteArray(result);
        // TODO 上线则删除该判断
        if (dianlan.getFrameSign() != DianlanFrameSign.HeartBeatResponse) {
            log.info("发出原始报文:" + toHex(ByteBuffer.wrap(b)));
        }
        try {
            session.writeBuffer().write(b);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void response(Dianlan dianlan, Object obj) {
        // TODO 上线则删除该判断
        if (dianlan.getFrameSign() != DianlanFrameSign.HeartBeat) {
            log.info("发出报文类型：" + dianlan.getFrameSign().getMessage());
            log.info("data:" + JSON.toJSONString(obj));
        }
        String data = DianlanProtocolParse.getData(obj);
        dianlan.setData(data);
    }

    public static String toHex(ByteBuffer bb) {
        StringBuilder sb = new StringBuilder("[ ");
        while (bb.hasRemaining()) {
            sb.append(String.format("%02X ", bb.get()));
        }//from  w w  w.  j  a va  2  s .  c  o  m
        sb.append("]");
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = "6822000100010000000000000100020F56342E312E353000010101010101010101010104BF6B";
        s = "68AE16E800ED00 00 00 00 00 00 01 00 02 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9D 0B";
        // 6818000100010000000000000100
        byte[] b = HexUtil.hexStringToByteArray(s);
        System.out.println(HexUtil.hexToBinary(s));
    }
}
